//
//  TagViewController.m
//  帮惠Mall
//
//  Created by reew on 2017/9/23.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "TagViewController.h"
#import "TagCollectionViewCell.h"
#import "System.h"
#import "AddGoodsTableViewController.h"
@interface TagViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *tagCollectionView;
@property (nonatomic,copy) NSMutableArray *tagArr;
@property (nonatomic,strong) NSMutableArray *selctedTagArr;
@end

@implementation TagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    //添加手势，点击tableview 隐藏键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.tagCollectionView addGestureRecognizer:tapGestureRecognizer];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    if (_selctedTagArr == nil) {
        _selctedTagArr = [NSMutableArray array];
    }
    
    [self setupNav];
    [self setUpCollectionView];
}
- (void)setupNav{
    UIBarButtonItem *rightItem= [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    //    rightItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
- (void)save{

    NSUInteger index = self.navigationController.viewControllers.count-2;
    AddGoodsTableViewController *vc = self.navigationController.viewControllers[index];
    NSString *tagStr = [_selctedTagArr componentsJoinedByString:@","];
    vc.tagStr = tagStr;
    vc.tagCell.detailTextLabel.text = tagStr;
    [self .navigationController popViewControllerAnimated:YES];
}
- (void)setUpCollectionView{
    self.tagCollectionView.delegate = self;
    self.tagCollectionView.dataSource = self;
    self.tagCollectionView.scrollEnabled = NO;
    //    [self.tagCollectionView registerClass:[TagCollectionViewCell class] forCellWithReuseIdentifier:@"tagCollectionViewCell"];
    [self.tagCollectionView registerNib:[UINib nibWithNibName:@"TagCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"TagCollectionViewCell"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    NSArray *tempArr = @[@"厂家直销",@"爆款",@"疯抢",@"自营",@"包邮",@"国庆",@"新品",@"国庆优惠",@"满赠",@"限时特卖"];
    _tagArr = [NSMutableArray arrayWithArray:tempArr];
}
- (void)hideKeyBoard{
    [self.tagCollectionView endEditing:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _tagArr.count +1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TagCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row == _tagArr.count) {
        cell.customTag.hidden = NO;
        cell.tagName.hidden= YES;
        cell.customTag.delegate = self;
    }else{
        cell.customTag.hidden = YES;
        cell.tagName.hidden= NO;
        cell.tagName.text = _tagArr[indexPath.row];
    }
    
    
    return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (!(textField.text == nil && textField.text.length ==0 )) {
        [_tagArr addObject:textField.text];
        NSIndexPath *index = [NSIndexPath indexPathForRow:_tagArr.count-1 inSection:0];
        [self.tagCollectionView insertItemsAtIndexPaths:@[index]];
    }
    textField.text = nil;
}
#pragma mark - UICollectionViewDataSource


#pragma mark - UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth-60)/3, 36);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 10, 5, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TagCollectionViewCell *item = (TagCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (item.haveSelected) {
        item.backgroundColor = [UIColor whiteColor];
        item.haveSelected = NO;
        [_selctedTagArr removeObject:item.tagName.text];
    }else{
        item.backgroundColor = [UIColor groupTableViewBackgroundColor];
        item.haveSelected = YES;
        [_selctedTagArr addObject:item.tagName.text];
    }
    NSLog(@"%@",_selctedTagArr);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
